package com.shfl.servermanage.logic.api;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.common.*;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.model.request.AddServerRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.SysExecutableFilePageResponse;
import com.shfl.servermanage.logic.model.response.SysServerPageResponse;
import com.shfl.servermanage.logic.service.SysServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/server")
@Api(value = "执行文件", description = "执行文件")
public class ServerController {

    @Autowired
    private SysServerService sysServerService;

    @GetMapping(value = "/page/list")
    public ApiResultPagingHelper<SysServerPageResponse, Void> page(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        PageInfo<SysServerPageResponse> pageInfo = sysServerService.findPage(requestHelper.getPageable(), objectMap);
        if (pageInfo.getTotal() > 0) {
            pageInfo.getList().forEach(item -> {
                item.setEndTimeValue(DateUtil.format(item.getEndTime(), DatePattern.NORM_DATETIME_PATTERN));
                item.setStartTimeValue(DateUtil.format(item.getStartTime(), DatePattern.NORM_DATETIME_PATTERN));
            });
        }
        return new ApiResultPagingHelper<>(SystemConstant.RE_SUCCESS_CODE,
                pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @GetMapping(value = "/list")
    public ApiResultListHelper<SysServerPageResponse> list(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        objectMap.put("deleteFlg", CommonConstants.DeleteFlg.NOT_DELETE.getCode());
        List<SysServerPageResponse> list = sysServerService.findList(objectMap);
        return new ApiResultListHelper<>(SystemConstant.RE_SUCCESS_CODE, list);
    }


    @PostMapping(value = "/add")
    @ApiOperation(value = "新增", notes = "新增")
    public ApiResultHelper add(
            @RequestBody AddServerRequest addServerRequest
    ) throws Exception {
        sysServerService.add(addServerRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @PostMapping(value = "/remove")
    @ApiOperation(value = "移除", notes = "移除")
    public ApiResultHelper remove(
            @RequestBody RemoveByIdRequest removeByIdRequest
    ) throws Exception {
        sysServerService.remove(removeByIdRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }
}
